package com.fenrirtec.aepp.common.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fenrirtec.aepp.common.dao.UserBidRecordsDao;
import com.fenrirtec.aepp.common.model.UserBidRecords;
import com.fenrirtec.core.database.DatabaseSessionManager;
import com.fenrirtec.core.database.ResultSetMapper;
import com.fenrirtec.core.exception.DatabaseException;

public class UserBidRecordsDao_JDBCImpl implements UserBidRecordsDao{
	private static final Logger logger = LoggerFactory.getLogger(TenderEnrollDao_JDBCImpl.class);
    @Override
    public List<UserBidRecords> searchByCnd(String memberLoginName) {
        if (logger.isInfoEnabled()) {
            logger.info("[UserBidRecordsDao_JDBCImpl#searchByCnd] start.");
        }

        PreparedStatement statement = null;
        
        try {
            
            StringBuilder sql = new StringBuilder()
            .append("select \n")
            .append("vti.tender_id, \n")
            .append("vti.tender_title, \n")
            .append("vti.tender_no, \n")
            .append("vti.status_name, \n")
            .append("tte.enroll_date, \n")           
            .append("tte.finalist_flag \n")          
            .append("from \n")
            .append("v_tender_information vti    \n")
            .append("join \n")
            .append("t_tender_enroll tte   \n")
            .append("on  \n")
            .append("(vti.tender_id = tte.tender_id) \n") 
            .append("where \n")
            .append("  tte.member_login_name = ? \n");
            
 
            
            sql.append("order by \n");
            sql.append("  vti.tender_id desc \n");
            
 
            
            if (logger.isTraceEnabled()) {
                logger.trace("[UserBidRecordsDao_JDBCImpl#searchByCnd] sql={}", sql);
            }
            
            Connection connection = DatabaseSessionManager.getInstance().getSession();
            statement = connection.prepareStatement(sql.toString());
            statement.setString(1, memberLoginName);

            ResultSet resultSet = statement.executeQuery();
            ResultSetMapper<UserBidRecords> resultSetMapper = new ResultSetMapper<UserBidRecords>();
            List<UserBidRecords> userBidRecordsList = resultSetMapper.mapResultSetToList(resultSet, UserBidRecords.class);
            
            return userBidRecordsList;
            
            
        } catch (SQLException e) {
            if (logger.isTraceEnabled()) {
                logger.trace("[UserBidRecordsDao_JDBCImpl#searchByCnd] sql error occurred.", e);
            }
            throw new DatabaseException("database error occurred.", e);
        } finally {
            if (logger.isInfoEnabled()) {
                logger.info("[UserBidRecordsDao_JDBCImpl#searchByCnd] finish.");
            }
            if (statement != null) { try { statement.close(); } catch (SQLException e) {} }
        }
    }
}
